//
//  CommentController.m
//  ProjectA
//
//  Created by lanouhn on 15/5/22.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "CommentController.h"
#import "ComSettingController.h"
#import "LoadingTableViewCell.h"
#import "ThirdCell.h"
#import "NetWorkEngine.h"
#import "AppDelegate.h"
#import "SelecteddController.h"
#define kURLThirt @"http://lol.video.luckyamy.com/api/?cat=jieshuo&page=%d&ap=lolg&ver=1.1"
@interface CommentController ()<NetworkEngineDelegate>
@property (nonatomic ,retain)UILabel *lable;
@property (nonatomic ,retain)UIBarButtonItem *item;
@property (nonatomic ,retain)NSMutableArray *dataSource;
@property (nonatomic ,assign)int pageNumber;
- (void)_startLoadingDataWithPage:(int)page;

@end

@implementation CommentController
- (void)dealloc {
    [_lable release];
    [_item release];
    [super dealloc];
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (UILabel *)lable {
    if (!_lable) {
        self.lable = [[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)]autorelease];
        _lable.text = @"解说";
        _lable.textAlignment = NSTextAlignmentCenter;
        _lable.textColor = [UIColor whiteColor];
    }
    return _lable ;
}
- (UIBarButtonItem *)item {
    if (!_item) {
        self.item = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"setting"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(handleItemSetting)];
    }
    return _item;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"解说";
    self.navigationItem.titleView = self.lable;
    self.navigationItem.rightBarButtonItem = self.item;
    [self.tableView registerClass:[ThirdCell class] forCellReuseIdentifier:@"Third"];
    [self.tableView registerClass:[LoadingTableViewCell class] forCellReuseIdentifier:@"LOAD"];
    self.refreshControl = [[[UIRefreshControl alloc]init]autorelease];
    [self.refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
    self.pageNumber = 1;
    [self _startLoadingDataWithPage:self.pageNumber];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}
- (void)_startLoadingDataWithPage:(int)page {
    NetworkEngine *engine = [NetworkEngine engineWithURL:[NSURL URLWithString:[NSString stringWithFormat:kURLThirt,self.pageNumber]] parameters:nil delegate:self];
    [engine start];
}
- (void)handleRefresh:(UIRefreshControl *)sender{
    self.pageNumber = 1;
    [self _startLoadingDataWithPage:self.pageNumber];
}
- (void)networkDidFinishLoading:(NetworkEngine *)engine withInfo:(id)info{
    [self.refreshControl endRefreshing];
    if (self.pageNumber == 1) {
        [self.dataSource removeAllObjects];
    }
    NSArray *array = [NSJSONSerialization JSONObjectWithData:info options: NSJSONReadingMutableContainers error:nil];
    for (NSDictionary *dic in array) {
        ThirdModel *model = [ThirdModel modelWithDic:dic];
        [self.dataSource addObject:model];
    }
    [self.tableView reloadData];
}
- (void)handleItemSetting{
    ComSettingController *setting = [[ComSettingController alloc]init];
    [self.navigationController pushViewController:setting animated:YES];
    [setting autorelease];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count + 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.dataSource.count) {
        return 30;
    }
    return 66;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.dataSource.count) {
        LoadingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LOAD" forIndexPath:indexPath];
        return cell;
    }
    ThirdCell *thirdCell = [tableView dequeueReusableCellWithIdentifier:@"Third" forIndexPath:indexPath];
    [thirdCell cellWithThirdModel:self.dataSource[indexPath.row]];
    return thirdCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SelecteddController *select = [[SelecteddController alloc]init];
    ThirdModel *model = self.dataSource[indexPath.row];
    select.ID = model.ID;
    select.tittle = model.title;
    [self.navigationController pushViewController:select animated:YES];
    [select release];
}
@end
